package com.n9.buymsg.service;

import com.n9.buymsg.AppContext;

import java.io.*;
import java.util.Optional;
import java.util.Properties;

/**
 * 配置文件
 * @Author: ice
 * @Date: 2020-06-24 10:23
 */
public class ConfigService {
    private static final String CONFIG_PATH = "config.properties";

    public void loadConfig() {
        Optional<Properties> opt = loadProperties();
        opt.ifPresent(pro -> {
            String account = pro.getProperty("account", "");
            String password = pro.getProperty("password", "");

            AppContext context = AppContext.getInstance();
            context.setAccount(account);
            context.setPassword(password);
        });
    }

    /**
     * 获取配置文件流
     * @return
     */
    private Optional<Properties> loadProperties() {
        File file = new File(CONFIG_PATH);
        if (file.exists()) {
            try {
                Properties pro = new Properties();
                pro.load(new FileInputStream(file));
                return Optional.of(pro);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return Optional.empty();
    }


    public void saveConfig() {

    }

}
